function dQ = GetdQ_4CC(wm)

ss = size(wm,1);
if ss == 2
    t1 = wm(1,:)';  t2 = wm(2,:)';
    phi = t1 + t2 + 2/3*cross(t1,t2);
elseif ss == 3
    t1 = wm(1,:)';  t2 = wm(2,:)';  t3 = wm(3,:)';
    phi = t1 + t2 + t3 ...
          + 27/40*cross(t1,t2) + 9/20*cross(t1,t3)  + 27/40*cross(t2,t3) ...
          + 261/2240*cross3(t1,t1,t2) + 27/448*cross3(t1,t1,t3) + 603/1120*cross3(t1,t2,t3) ...
          - 207/1120*cross3(t2,t1,t2) - 603/2240*cross3(t2,t1,t3) + 207/1120*cross3(t2,t2,t3) ...
          - 27/448*cross3(t3,t1,t3) - 261/2240*cross3(t3,t2,t3) ...
          + 37/19712*cross4(t1,t1,t1,t2) - 124/48691*cross4(t1,t1,t1,t3) + 659/4928*cross4(t1,t1,t2,t3) ...
          - 671/10151*cross4(t1,t2,t1,t2) - 2539/19647*cross4(t1,t2,t1,t3) + 1084/4493*cross4(t1,t2,t2,t3) ...
          - 635/19712*cross4(t1,t3,t1,t3) + 118/26253*cross4(t1,t3,t2,t3) + 71/8983*cross4(t2,t1,t1,t3) ...
          - 1084/4493*cross4(t2,t1,t2,t3) + 195/8389*cross4(t2,t2,t1,t2) + 13/704*cross4(t2,t2,t1,t3) ...
          + 195/8389*cross4(t2,t2,t2,t3) + 139/11211*cross4(t2,t3,t1,t3) - 671/10151*cross4(t2,t3,t2,t3) ...
          - 247/1787*cross4(t3,t1,t2,t3) - 124/48692*cross4(t3,t3,t1,t3) + 37/19712*cross4(t3,t3,t2,t3) ;
elseif ss == 4
    t1 = wm(1,:)';  t2 = wm(2,:)';  t3 = wm(3,:)';  t4 = wm(4,:)';
	vec_dot = [dot(t1,t1); dot(t2,t2); dot(t3,t3); dot(t4,t4); dot(t1,t2); dot(t1,t3); dot(t1,t4); dot(t2,t3); dot(t2,t4); dot(t3,t4)];
    c12 = [  32/40095  -83/1920  -271/9632  -49/23607   247/3226   133/2327  151/2095   144/1057  199/7974  -48/1043  ];
    c13 = [ -16/61613 -321/2041   155/23166 163/21685   323/6248    74/2747  533/15429   83/2376 1427/7725  117/1739  ];
    c14 = [ -27/12272 -219/2366  -325/5652  -27/12272   122/2467   184/7511  396/8287 -1260/7759   -5/183   135/26173 ];
    c23 = [-290/3641  -154/14933 -154/14933 114/20029   424/2489   389/3460 -589/4815   249/4246  226/10135  68/513   ];
    c24 = [ -121/3290  155/23166 -441/3691  -16/61613   643/6283  1131/3053  273/3161   254/2031   74/2747 -206/6123  ];
    c34 = [ -238/5885 -248/2079   -83/1920   32/40095 -1089/4699 -124/12273  558/4795   372/3779  225/1579  247/3226  ];
    A12 = c12*vec_dot; A13 = c13*vec_dot; A14 = c14*vec_dot; A23 = c23*vec_dot; A24 = c24*vec_dot;  A34 = c34*vec_dot;
    phi = t1 + t2 + t3 + t4 ...
          + 232/315*cross(t3,t4) + 46/105*cross(t2,t4) + 18/35*cross(t1,t4) + 178/315*cross(t2,t3) + 46/105*cross(t1,t3) + 232/315*cross(t1,t2)...
          + 591/5179*cross3(t1,t1,t2) - 753/3056*cross3(t2,t1,t2) -  499/908*cross3(t3,t1,t2) - 457/1138*cross3(t4,t1,t2)...
          + 265/3934*cross3(t1,t1,t3) + 599/1792*cross3(t2,t1,t3) - 109/3719*cross3(t3,t1,t3) - 457/1138*cross3(t4,t1,t3)...
          + 156/1571*cross3(t1,t1,t4) + 409/4783*cross3(t2,t1,t4) + 769/2433*cross3(t3,t1,t4) - 156/1571*cross3(t4,t1,t4)...
          +  111/823*cross3(t2,t2,t3) -  111/823*cross3(t3,t2,t3) -  499/908*cross3(t4,t2,t3) + 109/3719*cross3(t2,t2,t4)...
          + 411/1909*cross3(t3,t2,t4) - 265/3934*cross3(t4,t2,t4) + 753/3056*cross3(t3,t3,t4) - 591/5179*cross3(t4,t3,t4)...
          + A12*cross(t1,t2) + A13*cross(t1,t3) + A14*cross(t1,t4) + A23*cross(t2,t3) + A24*cross(t2,t4) + A34*cross(t3,t4);
elseif ss == 5
    t1 = wm(1,:)';  t2 = wm(2,:)';  t3 = wm(3,:)';  t4 = wm(4,:)';  t5 = wm(5,:)';
    phi = t1 + t2 + t3 + t4 + t5 ...
          + 232/315*cross(t3,t4) + 46/105*cross(t2,t4) + 18/35*cross(t1,t4) + 178/315*cross(t2,t3) + 46/105*cross(t1,t3) + 232/315*cross(t1,t2)...
          + 591/5179*cross3(t1,t1,t2) - 753/3056*cross3(t2,t1,t2) -  499/908*cross3(t3,t1,t2) - 457/1138*cross3(t4,t1,t2)...
          + 265/3934*cross3(t1,t1,t3) + 599/1792*cross3(t2,t1,t3) - 109/3719*cross3(t3,t1,t3) - 457/1138*cross3(t4,t1,t3)...
          + 156/1571*cross3(t1,t1,t4) + 409/4783*cross3(t2,t1,t4) + 769/2433*cross3(t3,t1,t4) - 156/1571*cross3(t4,t1,t4)...
          +  111/823*cross3(t2,t2,t3) -  111/823*cross3(t3,t2,t3) -  499/908*cross3(t4,t2,t3) + 109/3719*cross3(t2,t2,t4)...
          + 411/1909*cross3(t3,t2,t4) - 265/3934*cross3(t4,t2,t4) + 753/3056*cross3(t3,t3,t4) - 591/5179*cross3(t4,t3,t4);
elseif ss == 6
    t1 = wm(1,:)';  t2 = wm(2,:)';  t3 = wm(3,:)';  t4 = wm(4,:)';  t5 = wm(5,:)';  t6 = wm(5,:)';
    phi = t1 + t2 + t3 + t4 + t5 + t6 ...
          + 232/315*cross(t3,t4) + 46/105*cross(t2,t4) + 18/35*cross(t1,t4) + 178/315*cross(t2,t3) + 46/105*cross(t1,t3) + 232/315*cross(t1,t2)...
          + 591/5179*cross3(t1,t1,t2) - 753/3056*cross3(t2,t1,t2) -  499/908*cross3(t3,t1,t2) - 457/1138*cross3(t4,t1,t2)...
          + 265/3934*cross3(t1,t1,t3) + 599/1792*cross3(t2,t1,t3) - 109/3719*cross3(t3,t1,t3) - 457/1138*cross3(t4,t1,t3)...
          + 156/1571*cross3(t1,t1,t4) + 409/4783*cross3(t2,t1,t4) + 769/2433*cross3(t3,t1,t4) - 156/1571*cross3(t4,t1,t4)...
          +  111/823*cross3(t2,t2,t3) -  111/823*cross3(t3,t2,t3) -  499/908*cross3(t4,t2,t3) + 109/3719*cross3(t2,t2,t4)...
          + 411/1909*cross3(t3,t2,t4) - 265/3934*cross3(t4,t2,t4) + 753/3056*cross3(t3,t3,t4) - 591/5179*cross3(t4,t3,t4);
end

dQ = rv2q(phi);

end